<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>表单模块 - layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="__PUBLIC__/build/css/layui.css">

    <style>
        body{padding: 10px;}
    </style>
</head>
<body>

<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">访问授权</li>
        <li>操作授权</li>
    </ul>
    <div class="layui-tab-content" style="height: 100px;">
        <div class="layui-tab-item layui-show">
            <form class="layui-form layui-form-pane1 menu_form" action="">
                <div class="menuAuth">

                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" name="pk_id" value="{$_GET['pk_id']}">
                        <button class="layui-btn" lay-submit lay-filter="menu_submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form layui-form-pane1 access_form" action="">
                <div class="accessAuth">

                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" name="pk_id" value="{$_GET['pk_id']}">
                        <button class="layui-btn" lay-submit lay-filter="access_submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>



<br><br><br>


<script src="__PUBLIC__/build/layui.js"></script>
<!-- <script src="../build/lay/dest/layui.all.js"></script> -->

<script>
    layui.use(['form','element'], function(){
        var form = layui.form(),$ = layui.jquery;

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题也太短了吧';
                }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
        });

        form.on('checkbox', function(data){
            var check_box = $(data.othis).next().find("input[type='checkbox']");
            if(check_box.length > 0){
                check_box.prop('checked', data.elem.checked);
                form.render('checkbox')
            }
        });

        //用来显示的字段
        $(function () {
            $.get("getMenuTree?pk_id={$_GET['pk_id']}", '', function (data) {
                var obj = $('.menuAuth').empty();
                $.each(data.rows , function (key,val) {
                    var divHtml = '<div>' +
                                    '<input type="checkbox" name="menu[]" value="'+val['pk_id']+'" '+ (data.info['menu_auth'].indexOf(val['pk_id']) !== -1 ? 'checked="checked"' : '') +' lay-skin="primary" title="'+val['name']+'">' +
                                    '<div class="layui-form-item" pane>';
                    if(val['son']){
                        divHtml += '<div style="margin-left: 25px;">';
                        $.each(val['son'], function (sk,sv) {
                            divHtml += '<input type="checkbox" name="menu[]" value="'+sv['pk_id']+'" '+ (data.info['menu_auth'].indexOf(sv['pk_id']) !== -1 ? 'checked="checked"' : '') +' lay-skin="primary" title="'+sv['name']+'">';
                            if(sv['son']){
                                divHtml += '<div style="margin-left: 25px;">';
                                $.each(sv['son'], function (k,v) {
                                    divHtml += '<input type="checkbox" name="menu[]" value="'+v['pk_id']+'" '+ (data.info['menu_auth'].indexOf(v['pk_id']) !== -1 ? 'checked="checked"' : '') +' lay-skin="primary" title="'+v['name']+'">';
                                });
                                divHtml += '</div>';
                            }
                        });
                        divHtml += '</div>';
                    }
                    divHtml += '</div>';
                    obj.append(divHtml);
                });
                form.render();
            });
            getAccessTree();
        });

        function getAccessTree(){
            $.get("getAccessTree?pk_id={$_GET['pk_id']}", '', function (data) {
                var obj = $('.accessAuth').empty();
                $.each(data.rows , function (key,val) {
                    var divHtml = '<div>' +
                            '<input type="checkbox" name="access[]" value="'+val['pk_id']+'" '+ (data.info['access_auth'].indexOf(val['pk_id']) !== -1 ? 'checked' : '') +' lay-skin="primary" title="'+val['name']+'">' +
                            '<div class="layui-form-item" pane>';
                    if(val['son']){
                        divHtml += '<div style="margin-left: 25px;">';
                        $.each(val['son'], function (sk,sv) {
                            divHtml += '<input type="checkbox" name="access[]" value="'+sv['pk_id']+'" '+ (data.info['access_auth'].indexOf(sv['pk_id']) !== -1 ? 'checked' : '') +' lay-skin="primary" title="'+sv['name']+'">';
                            if(sv['son']){
                                divHtml += '<div style="margin-left: 25px;">';
                                $.each(sv['son'], function (k,v) {
                                    divHtml += '<input type="checkbox" name="access[]" value="'+v['pk_id']+'" '+ (data.info['access_auth'].indexOf(v['pk_id']) !== -1 ? 'checked' : '') +' lay-skin="primary" title="'+v['name']+'">';
                                });
                                divHtml += '</div>';
                            }
                        });
                        divHtml += '</div>';
                    }
                    divHtml += '</div>';
                    obj.append(divHtml);
                });
                form.render();
            })
        }

        //监听提交
        form.on('submit(menu_submit)', function(data){
            $.post("access",$('.menu_form').serialize(), function (data) {
                layer.msg(data.msg);
                getAccessTree();
            });
            return false;
        });

        //监听提交
        form.on('submit(access_submit)', function(data){
            $.post("access",$('.access_form').serialize(), function (data) {
                layer.msg(data.msg);
            });
            return false;
        });
    });
</script>

</body>
</html>
